package Midium;

import java.util.*;

public class Solution142 {
    class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    public ListNode detectCycle(ListNode head) {
        Set<ListNode> set = new HashSet<ListNode>();
        int count = 0;
        ListNode a = head;
        while(a!= null){
            if(set.contains(a)){
                return a;
            }
            set.add(a);
            a = a.next;
        }
        return null;
    }
}
